package com.myteam.mystory.DAO;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.myteam.mystory.entity.AuthorEntity;
import com.myteam.mystory.utils.LogUtils;

public class AuthorDAO {
	private static final String TAG = "AuthorDAO";
	private final SQLiteDatabase db;  //co bien final thi phai co ham khoi tao chua bien final
	
	
	
	public AuthorDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}

	public long insert_author(AuthorEntity authorEntity){
		ContentValues value = new ContentValues();
		value.put(AuthorScheme.AUTHORID, authorEntity.getAuthorID());
		value.put(AuthorScheme.FNAME, authorEntity.getFName());
		value.put(AuthorScheme.LNAME, authorEntity.getLName());
		value.put(AuthorScheme.ACODE, authorEntity.getACode());
		if(db.insert(AuthorScheme.TABLE_AUTHOR_NAME, null, value) == -1){
			LogUtils.e("Bug insert author");
		} else {
			LogUtils.e("Successfully insert author");
		}
		return 0;
	}
			
	public ArrayList<AuthorEntity> getAllAuthor(){
		ArrayList<AuthorEntity> list_author = new ArrayList<AuthorEntity>();
		String query = "select * from "+AuthorScheme.TABLE_AUTHOR_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if(cursor.moveToFirst()) {
			do {
				AuthorEntity authorEntity = new AuthorEntity();
				authorEntity.setAuthorID(cursor.getInt(0));
				authorEntity.setFName(cursor.getString(1));
				authorEntity.setLName(cursor.getString(2));
				authorEntity.setACode(cursor.getString(3));
				list_author.add(authorEntity);
			} while(cursor.moveToNext());
		}
		return list_author;
	}	
	
	public List<String> getLNameAuthor(){
		List<String> list_lastname = new ArrayList<String>();
		String query = "select * from "+AuthorScheme.TABLE_AUTHOR_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if(cursor.moveToFirst()){
			do {
				String lastName = cursor.getString(2);
				list_lastname.add(lastName);
			} while (cursor.moveToNext());
		}
		return list_lastname;
	}
	
	
			
	public long deleteTableAuthor(){
		long result = 0;
		try {
			result = db.delete(AuthorScheme.TABLE_AUTHOR_NAME, null, null);
			LogUtils.e("delete table author successfully");
		} catch (Exception e) {
			LogUtils.e("delete table author fail");
		}
		return result;	
		
	}	
			
			
			
			
			
			
			
			
}
